package com.xxx.mall.api.admin;

import com.xxx.mall.constant.Constant;
import com.xxx.mall.context.AuthUserContext;
import com.xxx.mall.dto.CategoryDTO;
import com.xxx.mall.group.Update;
import com.xxx.mall.response.R;
import com.xxx.mall.service.CategoryService;
import com.xxx.mall.vo.CategoryVO;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 分类信息 前端控制器
 * </p>
 *
 * @author 邢晨旭
 * @since 2023-11-15
 */
@RestController
@RequestMapping("/admin/category")
public class CategoryApi {

    @Resource
    CategoryService categoryService;

    /**
     * 获取平台端的分类列表
     *
     * @return 分类列表
     */
    @GetMapping("/platform_category")
    public R<List<CategoryVO>> platformCategory() {
        return R.ok(categoryService.queryList(Constant.PLATFORM_SHOP_ID));
    }

    @GetMapping("/platform_first_category")
    public R<List<CategoryVO>> platformFirstCategory() {
        return R.ok(categoryService.queryPlatformFirstCategory(Constant.PLATFORM_SHOP_ID,0));
    }

    /**
     * 获取店铺的分类列表
     *
     * @return 分类列表
     */
    @GetMapping("/shop_category")
    public R<List<CategoryVO>> shopCategory() {
        return R.ok(categoryService.queryList(AuthUserContext.get().getTenantId()));
    }

    @GetMapping
    public R<List<CategoryVO>> levelList(@RequestParam("level") Integer level) {
        return R.ok(categoryService.queryListByLevel(AuthUserContext.get().getTenantId(), level));
    }

    @GetMapping("/c/{id}")
    public R<List<CategoryVO>> ListByPId(@PathVariable Long id) {
        return R.ok(categoryService.queryListByParentId(AuthUserContext.get().getTenantId(), id));
    }

    @GetMapping("/p/{id}")
    public R<CategoryVO> ListByCId(@PathVariable Long id) {
        return R.ok(categoryService.queryCategoryById(id));
    }

    @GetMapping("/shop_category/tree")
    public R<List<CategoryVO>> shopCategoryTree() {
        return R.ok(categoryService.queryTreeList(AuthUserContext.get().getTenantId()));
    }

    @PutMapping
    public R<Void> update(@RequestBody @Validated(Update.class) CategoryDTO categoryDTO) {
        categoryService.updateCategoryById(categoryDTO);
        return R.ok();
    }

    @DeleteMapping("/{id}")
    public R<Void> delete(@PathVariable Long id) {
        categoryService.deleteById(id);
        return R.ok();
    }

    @PostMapping
    public R<Void> save(@RequestBody @Valid CategoryDTO categoryDTO) {
        categoryService.saveCategory(categoryDTO);
        return R.ok();
    }

    @GetMapping("/{id}")
    public R<CategoryVO> getById(@PathVariable Long id) {
        return R.ok(categoryService.queryById(id));
    }

    @PutMapping("/status")
    public R<Void> updateStatus(@RequestParam Long id, @RequestParam Integer status) {
        categoryService.categoryEnableOrDisable(id, status);
        return R.ok();
    }

}
